<?

class osWindows extends osGeneric {
	public $id = 'windows';

	public $device;
	public $tabs = array(
		'system' => 'System',
		'ipstats' => 'IP Statistics',
		'interfaces' => 'Ports',
		'processes' => 'Processes',
		'software' => 'Installed Software'
	
	);
	
	public $snmpSections = array(
		'HOST-RESOURCES-MIB',
		'IF-MIB',
		'SNMPv2-MIB',
		'TCP-MIB',
		'IP-MIB',
		'UDP-MIB'
	);
	
	public $mibs = array(
		'MSFT-MIB.mib'
		
	);	
	public $dataCollectors = array(
		'getProcessorLoads',
		'getProcessorUsage',
		'getMemoryUsed',
		'getSystemLoad',
		'getStorageUsage',
		'setProcessesUsersUptimeRrd'
	);
	
	public function __construct(&$device=null) { $this->device = $device; }
	
	public function getResources() {
		// Device Information
		foreach ($this->device->snmp->find('HOST-RESOURCES-MIB','hrDeviceIndex') AS $key=>$index) {
			$item = $this->device->snmp->find('HOST-RESOURCES-MIB','hrDevice([a-zA-z0-9]+).'.$index,true);
			$item = $item[$index];
			$sysDescr = $this->device->snmp->get('SNMPv2-MIB','sysDescr.0');

			switch (substr($item['hrDeviceType'],strrpos(':',$item['hrDeviceType']))) {
				case 'hrDeviceProcessor':
					$item['hrProcessorLoad'] = $this->device->snmp->get('HOST-RESOURCES-MIB','hrProcessorLoad.'.$index);
					$item['sysDescr'] = $sysDescr;
					$this->device->cpus[]  = new objProcessor($item);
					break;
			}
		}
		
		// Storage/Memory Information
		foreach ($this->device->snmp->find('HOST-RESOURCES-MIB','hrStorageIndex') AS $key=>$index) {
			$item = $this->device->snmp->find('HOST-RESOURCES-MIB','hrStorage([a-zA-z0-9]+).'.$index,true);
			$item = $item[$index];
			switch (substr($item['hrStorageType'],strrpos(':',$item['hrStorageType']))) {
				case 'hrStorageRam':
				case 'hrStorageVirtualMemory':
					$this->device->memory[$index] = new objMemory($item);
					break;
				case 'hrStorageFixedDisk':
				case 'hrStorageCompactDisc':
					$this->device->storage[$index] = new objWindowsStorage($item);
					break;
					
			}
		}
		// Network Interface Information
		foreach ($this->device->snmp->find('IF-MIB','ifIndex.([0-9]+)') AS $key=>$index) {
			$item = $this->device->snmp->find('IF-MIB','if([a-zA-z0-9]+).'.$index,true);
			$item = $item[$index];
			$item['device_id'] = $this->device->id;
			$interface = new objWindowsInterface($item);
			$this->device->interfaces[$index] = $interface;
		}	
	}
	public function getUptime() { return $this->device->snmp->get('HOST-RESOURCES-MIB','hrSystemUptime.0');}
	public function checkDescription($sysDescr='') { 
		return preg_match("/Windows/", $sysDescr)?$this->id:'';
	}	
	public function getType() { return $this->device->os_type; }
	public function getDescription() { 
		$sysDescr = $this->device->snmp->get('SNMPv2-MIB','sysDescr.0');
		if(strstr($sysDescr, "Windows 2000 Version 5.1")) {
			return "Windows XP";
		} elseif(strstr($sysDescr, "Windows Version 5.2")) {
			return "Windows 2003 Server";
		} elseif (strstr($sysDescr, "Windows Version 6.1")) {
			$output = array();
			$output[] = 'Windows 7';
			if(strstr($sysDescr, "x86")) {
				$output[] = '32bit(x86)';
			} elseif(strstr($sysDescr, "AMD64")) {
				$output[] = '64bit(x64)';
			}
			if(preg_match("/Build ([0-9]+)/", $sysDescr,$matches)) {
				$output[] = 'Build: '.$matches[1];
			}
			return implode(' ',$output);
		} elseif(strstr($sysDescr, "x86")) {
			return "Generic x86";
		} elseif(strstr($sysDescr, "AMD64")) {
			return "Generic x64";
		} else {
			return 'Windows Unknown';
		}	
	}
	public function getProcessorUsage() {
		$this->device->snmp->getData('.1.3.6.1.4.1.2021.11');
		$path = $this->device->getRrdPath().'processor_usage.rrd';
		if (!is_file($path)) {
		   $opts = array(
				'--step',$this->device->poll_freq,
				'DS:user:COUNTER:600:0:U',
				'DS:system:COUNTER:600:0:U',
				'DS:nice:COUNTER:600:0:U',
				'DS:idle:COUNTER:600:0:U',
				'RRA:AVERAGE:0.5:1:800',
				'RRA:AVERAGE:0.5:6:800',
				'RRA:AVERAGE:0.5:24:800',
				'RRA:AVERAGE:0.5:288:800',
				'RRA:MAX:0.5:1:800',
				'RRA:MAX:0.5:6:800',
				'RRA:MAX:0.5:24:800',
				'RRA:MAX:0.5:288:800'
			);
			if (rrd_create($path,$opts,count($opts)) == 0) {
		   		echo 'Error: '.rrd_error().chr(10);
				// Log Error
				return false;
		   }
		}
		$rrd = array();
		$rrd[] = 'N';
		$rrd[] = $this->device->snmp->exists('UCD-SNMP-MIB','ssCpuRawUser.0')?$this->device->snmp->get('UCD-SNMP-MIB','ssCpuRawUser.0'):0;
		$rrd[] = $this->device->snmp->exists('UCD-SNMP-MIB','ssCpuRawSystem.0')?$this->device->snmp->get('UCD-SNMP-MIB','ssCpuRawSystem.0'):0;
		$rrd[] = $this->device->snmp->exists('UCD-SNMP-MIB','ssCpuRawNice.0')?$this->device->snmp->get('UCD-SNMP-MIB','ssCpuRawNice.0'):0;
		$rrd[] = $this->device->snmp->exists('UCD-SNMP-MIB','ssCpuRawIdle.0')?$this->device->snmp->get('UCD-SNMP-MIB','ssCpuRawIdle.0'):0;
		echo 'Processor Usage ('.$this->device->id.'): '.implode(':',$rrd).chr(10);
		if (rrd_update($path,implode(':',$rrd)) == 0) {
			echo rrd_error();
			return false;
		} else {
			return true;
		}
	}

	
	
	public function getSoftwareInstalled() {
		foreach ($this->device->snmp->find('HOST-RESOURCES-MIB','hrSWInstalledIndex') AS $oid=>$index) {
			$package = $this->device->snmp->find('HOST-RESOURCES-MIB','hrSWInstalled([a-zA-z]+).'.$index,true);
			$package = $package[$index];
			$this->device->software[$index] = new objSoftware($package);
		}
	}
	
	public function sortSoftware($field='installed',$dir='DESC') {
		$tosort = array();
		$output = array();
		foreach ($this->device->software AS $index=>$software) {
			$tosort[$index] = $software->$field;		
		}
		if ($dir == 'ASC') {
			asort($tosort);
		} else {
			arsort($tosort);
		}
		foreach ($tosort AS $index=>$value) {
			$output[$index] = $this->device->software[$index];
		}
		return $output;
	}
}
